import { createApp } from 'vue';
import DeleteComponent from './delete.vue';

let instance = null;
let container = null;

export default {
  install(app) {
    const createDelete = () => {
      if (!instance) {
        container = document.createElement('div');
        document.body.appendChild(container);
        instance = createApp(DeleteComponent);
        instance = instance.mount(container);
      }
      return instance;
    };

    const deleteConfirm = (name, confirmCallback, cancelCallback = () => { }) => {
      const deleteInstance = createDelete();
      if (deleteInstance.visible) return;
      deleteInstance.name = name;
      deleteInstance.visible = true;
      deleteInstance.confirm = function () {
        deleteInstance.visible = false;
        confirmCallback();
      };
      deleteInstance.cancel = function () {
        deleteInstance.visible = false;
        cancelCallback();
      };
    };

    window.$delete = deleteConfirm;
    app.config.globalProperties.$delete = deleteConfirm;
  }
}
